package com.example.file.utils;

import java.nio.file.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class FileTypeUtil {

    // 支持的图片扩展名
    private static final Set<String> IMAGE_EXTENSIONS = new HashSet<>(Arrays.asList(
            "jpg", "jpeg", "png", "gif", "bmp", "tiff", "webp","svg"
    ));

    // 支持的视频扩展名
    private static final Set<String> VIDEO_EXTENSIONS = new HashSet<>(Arrays.asList(
            "mp4", "avi", "mkv", "mov", "flv", "wmv", "webm", "mpeg", "mpg"
    ));

    private static final String XML_EXTENSIONS = "xml";

    private static final String HTML_EXTENSIONS = "html";

    private static final Set<String> WORD_EXTENSIONS = new HashSet<>(Arrays.asList(
            "doc", "docx"
    ));

    private static final Set<String> EXCEL_EXTENSIONS = new HashSet<>(Arrays.asList(
            "xlsx", "xls"
    ));

    private static final Set<String> TEXT_EXTENSIONS = new HashSet<>(Arrays.asList(
            "txt", "log"
    ));

    private static final String PDF_EXTENSIONS = "pdf";
    private static final String JACA_EXTENSIONS = "java";
    private static final String CSS_EXTENSIONS = "css";
    private static final String JS_EXTENSIONS = "js";
    private static final String JSON_EXTENSIONS = "json";
    private static final String VUE_EXTENSIONS = "vue";
    private static final String PROPERTIES_EXTENSIONS = "properties";

    private static final Set<String> YML_EXTENSIONS = new HashSet<>(Arrays.asList(
            "yml", "yaml"
    ));

    /**
     * 判断文件类型
     *
     * @param filePath 文件路径
     * @return 1：目录 2：图片 3：视频 4：其他文件
     */
    public static int getFileType(String filePath) {
        Path path = Paths.get(filePath);

        // 判断是否是目录
        if (Files.isDirectory(path)) {
            return 1; // 目录
        }

        // 判断文件是否是图片
        String fileExtension = getFileExtension(path);
        if (IMAGE_EXTENSIONS.contains(fileExtension.toLowerCase())) {
            return 2; // 图片
        }

        // 判断文件是否是视频
        if (VIDEO_EXTENSIONS.contains(fileExtension.toLowerCase())) {
            return 3; // 视频
        }

        if(XML_EXTENSIONS.equalsIgnoreCase(fileExtension)){
            return 5;//xml
        }

        if(HTML_EXTENSIONS.equalsIgnoreCase(fileExtension)){
            return 6;//html
        }

        if(WORD_EXTENSIONS.contains(fileExtension.toLowerCase())){
            return 7;
        }

        if(EXCEL_EXTENSIONS.contains(fileExtension.toLowerCase())){
            return 8;
        }

        if(TEXT_EXTENSIONS.contains(fileExtension.toLowerCase())){
            return 9;
        }

        if(PDF_EXTENSIONS.equalsIgnoreCase(fileExtension)){
            return 10;
        }

        if(JACA_EXTENSIONS.equalsIgnoreCase(fileExtension)){
            return 11;
        }

        if(JS_EXTENSIONS.equalsIgnoreCase(fileExtension)){
            return 12;
        }

        if(CSS_EXTENSIONS.equalsIgnoreCase(fileExtension)){
            return 13;
        }

        if(JSON_EXTENSIONS.equalsIgnoreCase(fileExtension)){
            return 14;
        }

        if(VUE_EXTENSIONS.equalsIgnoreCase(fileExtension)){
            return 15;
        }

        if(PROPERTIES_EXTENSIONS.equalsIgnoreCase(fileExtension)){
            return 16;
        }

        if(YML_EXTENSIONS.contains(fileExtension.toLowerCase())){
            return 17;
        }

        // 其他文件
        return 4; // 其他文件
    }

    /**
     * 获取文件扩展名
     *
     * @param path 文件路径
     * @return 文件扩展名（小写）
     */
    private static String getFileExtension(Path path) {
        String fileName = path.getFileName().toString();
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex > 0) {
            return fileName.substring(dotIndex + 1);
        }
        return ""; // 没有扩展名
    }

}
